﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Linq;
namespace PetroMentor.Service
{
    public class EntitiesManager<T>
    {
        public EntitiesManager()
        {
            
        }

        public virtual IList<T> Select(string hsql = "")
        {
            IList<T> list;

            ISession session = NHibernateHelper.Instance().GetSession();
            try
            {
                list = session.CreateQuery(hsql).List<T>();
                SelectOp();
                return list;
            }
            catch
            {
                return null;
            }
            finally
            {
                session.Close();
            }
       }

        public virtual IList<T> SelectBySql(string sql = "")
        {
            IList<T> list;
            ISession session=null;
           
            try
            {
                session = NHibernateHelper.Instance().GetSession();
                ISQLQuery query = session.CreateSQLQuery(sql).AddEntity(typeof(T));
                list = query.List<T>();
                SelectOp();
                return list;
            }
            catch
            {
                return null;
            }
            finally
            {
                session.Close();
            }
        }

        public virtual decimal SelectResultCount(string sql = "")
        {
            decimal count;

            ISession session = NHibernateHelper.Instance().GetSession();
            try
            {
                ISQLQuery query = session.CreateSQLQuery(sql);
                count = query.UniqueResult<decimal>();
                return count;
            }
            catch
            {
                return 0;
            }
            finally
            {
                session.Close();
            }
        }

        
        
        public virtual bool Add(IList<T> infos)
        {
            
            ISession session = NHibernateHelper.Instance().GetSession();
            ITransaction transaction = session.BeginTransaction();

            try
            {
                foreach (T info in infos)
                {
                    session.Save(info);
                    AddOp(info);
                }
                transaction.Commit();
                return true;
            }
            catch
            {
                transaction.Rollback();
                return false;
            }

            finally
            {
                session.Close();
            }
            
        }

        public virtual void Add(T info)
        {
            ISession session = NHibernateHelper.Instance().GetSession();
            ITransaction transaction = session.BeginTransaction();

            try
            {

                session.Save(info);
                AddOp(info);
                transaction.Commit();
                
            }
            catch
            {
                transaction.Rollback();
                
            }

            finally
            {
                session.Close();
            }

        }


        protected virtual void SelectOp()
        { 
        
        }
        protected virtual void AddOp(T info)
        { 
        
        }
        protected virtual void UpdateOp(T info)
        {

        }

        /// <summary>
        ///  更新
        /// </summary>
        /// <returns></returns>
        public bool Update(IList<T> infos)
        {
            ISession session = NHibernateHelper.Instance().GetSession();
            ITransaction transaction = session.BeginTransaction();
            try
            {
                foreach (T info in infos)
                {
                    session.Update(info);
                    UpdateOp(info);
                }
                transaction.Commit();
                return true;
            }
            catch
            {
                transaction.Rollback();
                return false;
            }
            finally
            {
                session.Close();
            }
        }

        /// <summary>
        ///  更新
        /// </summary>
        /// <returns></returns>
        public bool Update(T info)
        {
            ISession session = NHibernateHelper.Instance().GetSession();
            ITransaction transaction = session.BeginTransaction();
            try
            {
                
                session.Update(info);
                UpdateOp(info);
                
                transaction.Commit();
                return true;
            }
            catch
            {
                transaction.Rollback();
                return false;
            }
            finally
            {
                session.Close();
            }
        }

        public virtual bool Delete(string module,string amodule,string ids)
        {
            try
            {
                ISQLQuery query;
                string sql ;
                ISession session = NHibernateHelper.Instance().GetSession();
                ITransaction transaction = session.BeginTransaction();

                sql = string.Format("DELETE FROM {0} t WHERE t.ID IN ({1})", module,ids);

                query = session.CreateSQLQuery(sql);
                query.ExecuteUpdate();

                if (!string.IsNullOrWhiteSpace(amodule))
                {
                    sql = string.Format("DELETE FROM {0} t WHERE t.OWNERID IN ({1})", module, ids);
                    session.CreateSQLQuery(sql);
                    query.ExecuteUpdate();
                }

                transaction.Commit();
               
                return true;
            }catch
            {
                return false;
            }
        }

        public virtual bool Delete(string id) 
        {
            try
            {
                ISession session = NHibernateHelper.Instance().GetSession();
                string sql = string.Format("DELETE FROM {0} t WHERE t.ID = {1}", typeof(T).Name, id);
                session.CreateQuery(sql).ExecuteUpdate();
                return true;
               
            }
            catch
            {
                return false;
            }

        }
    }
}